<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/8/3
 * Time: 15:52
 */

namespace app\command\company\data_change;

use app\company\model\Goods;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\admin\model\GoodsRule as  GoodsRuleModel;

class GoodsRule extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('data:change:5')->setDescription('基础商品库添加no字段,规格添加sku字段');
    }


    protected function execute(Input $input, Output $output)
    {
        $output->writeln('基础商品库添加no字段,规格添加sku字段开始');
        $model = new Goods();
        $model->startTrans();
        try {
            $model->withTrashed()->select()->each(function ($item) {
                $item->no = 'GOODS' . getPaddedNumber($item->id, 10);
                $item->save();
            });
            (new GoodsRuleModel())->withTrashed()->select()->each(function ($item) {
                $item->sku = getPaddedNumber($item->id, 9);
                $item->save();
            });
            $model->commit();
            $output->writeln('基础商品库添加no字段,规格添加sku字段成功');
        } catch (\Exception $e) {
            $model->rollback();
            $output->writeln('基础商品库添加no字段,规格添加sku字段失败' . $e->getMessage() . $e->getLine() . $e->getFile());
        }
    }
}